﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Wys.DataForm.CSharp
{
    public partial class CSharpMenu : Wys.Control.Vb.AutoAdjustFrom
    {
        public CSharpMenu()
        {
            InitializeComponent();
        }

        public TreeNode GetSelectNode()
        {
            return this.trvRole.SelectedNode;
        }

        public void LoadNodeData(TreeNode tn,String strMenuName)
        {
            ADODB.Recordset rs = null;
            rs = ct.GetTable("SELECT * FROM  XSCMenuBackUp where ParentMenu='" + strMenuName + "' AND IsParentMenu=1");
            if (rs != null)
            {
                while (!rs.EOF)
                {
                    TreeNode tnChild = new TreeNode();
                    tnChild.Text = rs.Fields["TITLE"].Value.ToString();
                    if (String.IsNullOrEmpty(rs.Fields["CLASSNAME"].Value.ToString()))
                    {
                        tnChild.Tag = rs.Fields["MENUNAME"].Value.ToString();
                        tn.Nodes.Add(tnChild);
                        LoadNodeData(tnChild, tnChild.Tag.ToString());
                    }                 
                    
                    rs.MoveNext();
                }
            }
            rs.Close();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(rs); //20100531
        }
        public void LoadData()
        {
            ADODB.Recordset rs = ct.GetTable("SELECT * FROM  XSCMenuBackUp where ParentMenu is null");
            if (rs != null)
            {
                while (!rs.EOF)
                {
                    TreeNode tn = new TreeNode();
                    tn.Text = rs.Fields["TITLE"].Value.ToString();
                    tn.Tag = rs.Fields["MENUNAME"].Value.ToString();
                    this.trvRole.Nodes.Add(tn);                 
                    rs.MoveNext();
                }
                rs.Close();
                //System.Runtime.InteropServices.Marshal.ReleaseComObject(rs); //20100531
                foreach (TreeNode tn in this.trvRole.Nodes)
                {                   
                    LoadNodeData(tn, tn.Tag.ToString());
                }

                (rs.ActiveConnection as ADODB.Connection).Close();
            }
        }

        private void CSharpMenu_Load(object sender, EventArgs e)
        {
            LoadData();
        }      

        private void btnOk_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
